/**
 * Copyright 2011 Google Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * @fileoverview Main javascript file for the application.
 * @author api.ekoleda@gmail.com (Eric Koleda)
 */

/**
 * Constructor for the page's controller.
 * @param {Function} $resource the Angular $resource function
 * @param {Function} $location the Angular $location function
 * @constructor
 */
function PageController($resource, $location) {
  this.Accounts = $resource(
      'get/accounts.php?canManageClients=:canManageClients',
      {'typeOfLink': 'ESTABLISHED'});
  this.Campaigns = $resource(
      'get/campaigns.php?customerId=:customerId&status=:status&time=:time',
      {'status': 'ACTIVE'});
  var auth = $.cookie('AW_SESSION_ACTIVE');
  if (auth == "true") {
    $('#logout').show();
    this.loadAccounts();
  } else {
    $('#login').show();
  }
  if ($location.search.error) {
    this.error = this.$location.search.error;
  }
}

/**
 * Inject dependencies into PageController.
 */
PageController.$inject = ['$resource', '$location'];

/**
 * Loads the ad serving accounts accessible to the logged in user.
 */
PageController.prototype.loadAccounts = function() {
  var scope = this;
  this.accountsLoading = true;
  var params = {
    'canManageClients': false
  };
  this.Accounts.get(params, function(result) {
    scope.accountsLoading = false;
    if (!result.error) {
      scope.rootAccount = result.rootAccount;
      scope.accounts = result.accounts;
      scope.customerId = result.accounts[0].customerId;
    } else {
      scope.error = result.error;
    }
  });
};

/**
 * Loads the campaigns in the currently selected account.
 * @param {boolean} refresh if true any cached data will be discarded and the
 *     campaigns will be loaded from the server
 */
PageController.prototype.loadCampaigns = function(refresh) {
  var scope = this;
  if (this.customerId) {
    this.campaignsLoading = true;
    this.campaigns = null;
    this.selectedCampaign = null;
    var params = {
      'customerId': this.customerId,
      'status': this.status
    };
    if (refresh) {
      params['time'] = new Date().getTime();
    }
    this.Campaigns.get(params, function(result) {
      scope.campaignsLoading = false;
      if (!result.error) {
        scope.campaigns = result.campaigns;
      } else {
        scope.error = result.error;
      }
    });
  }
};

/**
 * Shows the campaign details.
 * @param {Object} campaign the campaign to display
 */
PageController.prototype.showCampaign = function(campaign) {
  this.selectedCampaign = campaign;
};
